package com.example.master.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.io.*;
import java.util.*;

/**
 * 通用的返回结果类，后端所有的数据都回会封装为该对象返回给前端   用以统一格式
 * @param <T>
 */

@Data
@ApiModel(value = "统一响应格式")
public class Result<T> implements Serializable {

    /**
     * 状态码：1成功，0和其它数字为失败
     */
    @ApiModelProperty(value = "状态码：1成功，0和其它数字为失败")
    private Integer code;

    /**
     * 错误信息
     */
    @ApiModelProperty(value = "错误信息")
    private String msg;

    /**
     * 数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;

    /**
     * 动态数据
     */
    @ApiModelProperty(value = "动态数据")
    private Map map = new HashMap();

    public static Result success() {
        Result result = new Result();
        result.code = 1;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.msg = "操作成功";
        result.data = object;
        result.code = 1;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();

        result.msg = msg;
        result.code = 0;
        return result;
    }

    public Result<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
